package com.ys.gulimall.ware.service.impl;

import com.ys.common.to.SkuHasStockVo;
import com.ys.common.utils.R;
import com.ys.gulimall.ware.feign.ProductFeignService;
import com.ys.gulimall.ware.service.WareOrderTaskDetailService;
import com.ys.gulimall.ware.service.WareOrderTaskService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.common.utils.PageUtils;
import com.ys.common.utils.Query;

import com.ys.gulimall.ware.dao.WareSkuDao;
import com.ys.gulimall.ware.entity.WareSkuEntity;
import com.ys.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
	
	@Resource
	private WareSkuDao wareSkuDao;
	
	@Resource
	private ProductFeignService productFeignService;
	
	//@Resource
	//private RabbitTemplate rabbitTemplate;
	
	@Resource
	private WareOrderTaskService wareOrderTaskService;
	
	@Resource
	private WareOrderTaskDetailService wareOrderTaskDetailService;
	
	//@Resource 
	//private OrderFeignService orderFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
	    QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
	
	    String skuId = (String) params.get("skuId");
	    if (!StringUtils.isEmpty(skuId) && !"0".equalsIgnoreCase(skuId)) {
		    queryWrapper.eq("sku_id",skuId);
	    }
	
	    String wareId = (String) params.get("wareId");
	    if (!StringUtils.isEmpty(wareId) && !"0".equalsIgnoreCase(wareId)) {
		    queryWrapper.eq("ware_id",wareId);
	    }
	
	    IPage<WareSkuEntity> page = this.page(
			    new Query<WareSkuEntity>().getPage(params),
			    queryWrapper
	    );
	
	    return new PageUtils(page);
    }
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addStock(Long skuId, Long wareId, Integer skuNum) {
		
		//1、判读如果没有这个库存记录新增
		List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(
				new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
		
		if (wareSkuEntities == null || wareSkuEntities.size() == 0) {
			WareSkuEntity wareSkuEntity = new WareSkuEntity();
			wareSkuEntity.setSkuId(skuId);
			wareSkuEntity.setStock(skuNum);
			wareSkuEntity.setWareId(wareId);
			wareSkuEntity.setStockLocked(0);
			// 远程查询sku的名字，如果失败整个事务无需回滚
			//1、自己catch异常
			try{
				R info = productFeignService.info(skuId);
				Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
				if (info.getCode() == 0) {
					wareSkuEntity.setSkuName((String) data.get("skuName"));
				}
			} catch (Exception e) {
				
			}
			//添加库存信息
			wareSkuDao.insert(wareSkuEntity);
		} else {
			//修改库存信息
			wareSkuDao.addStock(skuId,wareId,skuNum);
		}
		
	}
	
	@Override
	public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
		List<SkuHasStockVo> skuHasStockVos = skuIds.stream().map(item -> {
			// select sum(stock - stock_locked) from wms_ware_sku where sku_id = ?
			Long count = this.baseMapper.getSkuStock(item);
			SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
			skuHasStockVo.setSkuId(item);
			skuHasStockVo.setHasStock(count == null?false:count > 0);
			return skuHasStockVo;
		}).collect(Collectors.toList());
		return skuHasStockVos;
	}
	
}